package org.adw.launcher2.ld.secret.db;


import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/*
 * Provide helper functions to interact with database
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	public static final String DB_NAME = "mysecret";
	SQLiteDatabase db;
	private static DatabaseHelper instance = null;

	public static DatabaseHelper getInstance(Context context) {
		if (instance == null)
			instance = new DatabaseHelper(context);

		return instance;
	}

	private DatabaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		
		db = context.openOrCreateDatabase(DB_NAME, 0, null);
	}

	public boolean isNotOpen() {
		return !db.isOpen();
	}

	@Override
	public void close() {
		synchronized (db) {
			if (db != null)
				db.close();
		}
		super.close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
	
	public boolean isTableExists(String tableName) {
		Cursor cursor = 
			db.rawQuery("SELECT * FROM SQLITE_MASTER WHERE NAME = '" + tableName + "'",
					null);
		if (cursor.getCount() == 0) {
			cursor.close();
			return false;
		}
		
		cursor.close();
		return true;
	}
	
}
